package com.cuihanyu.newuniversityover.adapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.activity.NoteBookDetailActivity;
import com.cuihanyu.newuniversityover.bean.SQLBean;
import com.cuihanyu.newuniversityover.sql.DBManger;
import com.cuihanyu.newuniversityover.sql.MySQLiteHelper;
import com.cuihanyu.newuniversityover.sql.SQLiteUtil;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/11.
 */

public class NoteAdapter extends BaseQuickAdapter<SQLBean, NoteAdapter.ViewHolder> {
    private MySQLiteHelper mhelper;
    private SQLiteDatabase db;
    private List<SQLBean> data;
    private Activity activity;
    private Intent intent;
    private Fragment fragment;

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public NoteAdapter(Activity activity, @Nullable List<SQLBean> data, Fragment fragment) {
        super(R.layout.fragment_book_sql_item, data);
        this.data = data;
        this.activity = activity;
        this.fragment = fragment;
    }

    @Override
    protected void convert(final ViewHolder helper, final SQLBean item) {
        helper.mChapter_TextView.setText(item.getChapter_name());
        helper.mChapter_Small.setText(item.getChapter_small());
        helper.mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                helper.mSwipeMenuLayout.quickClose();
                mhelper = DBManger.getIntance(activity);
                db = mhelper.getWritableDatabase();
                String delSql = "delete from " + SQLiteUtil.TABLE_NAME + " where " + SQLiteUtil.TIME + "='" + item.getTime() + "'";
                DBManger.execSQL(db, delSql);
                Cursor cursor = db.query(SQLiteUtil.TABLE_NAME, null, null, null, null, null, "_id desc");
                data.clear();
                //判断游标是否为空
                if (cursor.moveToFirst()) {
                    do {
                        String mTime = cursor.getString(cursor.getColumnIndex("time"));
                        String mChapter_name = cursor.getString(cursor.getColumnIndex("chapter_name"));
                        String mChapter_small = cursor.getString(cursor.getColumnIndex("chapter_small"));
                        String mNote = cursor.getString(cursor.getColumnIndex("note"));
                        data.add(new SQLBean(mTime, mChapter_name, mChapter_small, mNote));
                    } while (cursor.moveToNext());
                }
                cursor.close();
                db.close();
                notifyDataSetChanged();
            }
        });
        helper.mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (intent == null) intent = new Intent();
                intent.setClass(activity, NoteBookDetailActivity.class);
                intent.putExtra("time", item.getTime());
                intent.putExtra("chapter_name", item.getChapter_name());
                intent.putExtra("chapter_small", item.getChapter_small());
                intent.putExtra("note", item.getNote());
                fragment.startActivityForResult(intent, 1);
            }
        });
    }


    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.content)
        SwipeMenuLayout mSwipeMenuLayout;
        @BindView(R.id.ll)
        LinearLayout mLinearLayout;
        @BindView(R.id.Chapter_TextView)
        TextView mChapter_TextView;
        @BindView(R.id.Chapter_Small)
        TextView mChapter_Small;
        @BindView(R.id.delete)
        ImageView mDelete;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
